package com.chengyu.core.service.services.impl;

import com.alibaba.fastjson.JSONArray;
import com.chengyu.core.domain.enums.ServicesEnums;
import com.chengyu.core.domain.form.ServicesPublishSeckillForm;
import com.chengyu.core.domain.form.ServicesPublishSeckillPriceForm;
import com.chengyu.core.mapper.PmsServicesMapper;
import com.chengyu.core.mapper.PmsServicesSkuMapper;
import com.chengyu.core.model.PmsServices;
import com.chengyu.core.model.PmsServicesSku;
import com.chengyu.core.model.PmsServicesSkuExample;
import com.chengyu.core.model.UmsShop;
import com.chengyu.core.service.services.ServicesSeckillService;
import com.chengyu.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ServicesSeckillServiceImpl implements ServicesSeckillService {

	@Autowired
	private PmsServicesMapper servicesMapper;
	@Autowired
	private PmsServicesSkuMapper servicesSkuMapper;

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void addSeckillServices(UmsShop shop, ServicesPublishSeckillForm publishForm) {
		PmsServices updateServices = new PmsServices();
		updateServices.setId(publishForm.getId());
		updateServices.setType(ServicesEnums.ServicesType.SECKILL_GOODS.getValue());
		updateServices.setSeckillBeginTime(publishForm.getSeckillBeginTime());
		updateServices.setSeckillEndTime(publishForm.getSeckillEndTime());
		updateServices.setSeckillLimitNum(publishForm.getSeckillLimitNum());

		if(StringUtils.isNotBlank(publishForm.getPriceFormList())) {
			List<ServicesPublishSeckillPriceForm> priceFormList = JSONArray.parseArray(publishForm.getPriceFormList(), ServicesPublishSeckillPriceForm.class);
			for(ServicesPublishSeckillPriceForm priceForm : priceFormList) {
				PmsServicesSku sku = servicesSkuMapper.selectByPrimaryKey(priceForm.getSkuId());
				PmsServicesSku updateSku = new PmsServicesSku();
				updateSku.setId(priceForm.getSkuId());
				updateSku.setOriPrice(sku.getPrice());
				updateSku.setPrice(priceForm.getPrice());
				servicesSkuMapper.updateByPrimaryKeySelective(updateSku);
			}
			updateServices.setPrice(priceFormList.get(0).getPrice());
		}
		servicesMapper.updateByPrimaryKeySelective(updateServices);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteSeckill(List<Integer> servicesIdList) {
		//商品SKU恢复原价
		PmsServicesSkuExample skuExample = new PmsServicesSkuExample();
		skuExample.createCriteria().andServicesIdIn(servicesIdList);
		List<PmsServicesSku> skuList = servicesSkuMapper.selectByExample(skuExample);
		for(PmsServicesSku sku : skuList) {
			PmsServicesSku updateSku = new PmsServicesSku();
			updateSku.setId(sku.getId());
			updateSku.setPrice(sku.getOriPrice());
			servicesSkuMapper.updateByPrimaryKeySelective(updateSku);
		}

		for(Integer servicesId : servicesIdList) {
			PmsServices updateServices = new PmsServices();
			updateServices.setId(servicesId);
			updateServices.setType(ServicesEnums.ServicesType.NORMAL_GOODS.getValue());

			skuExample = new PmsServicesSkuExample();
			skuExample.setOrderByClause("id asc");
			skuExample.createCriteria().andServicesIdEqualTo(servicesId);
			List<PmsServicesSku> skus = servicesSkuMapper.selectByExample(skuExample);
			updateServices.setPrice(skus.get(0).getPrice());
			servicesMapper.updateByPrimaryKeySelective(updateServices);
		}
	}
}